package lumis.service.wsrp.producer;

import javax.persistence.NoResultException;

import org.w3c.dom.Node;

import lumis.doui.source.IDataProvider;
import lumis.doui.source.ISourceData;
import lumis.doui.source.TabularData;
import lumis.doui.source.TabularSource;
import lumis.doui.source.field.ITabularSourceField;
import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.localization.ILocalizationManager;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.serviceinterface.ServiceInterfaceConfig;
import lumis.portal.serviceinterfaceinstance.ServiceInterfaceInstanceConfig;
import lumis.util.ITransaction;
import lumis.util.XmlUtil;

/**
 * {@link IDataProvider} that reads the current service instance's
 * {@link ClonedPortletsListDataProvider} and if it exists populate a {@link TabularData} with the
 * right values for the interface.
 * 
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class ClonedPortletsListDataProvider implements IDataProvider<TabularSource<ITabularSourceField>>
{
	public void loadData(SessionConfig sessionConfig, TabularSource<ITabularSourceField> source, ITransaction transaction) throws PortalException
	{
		String registrationHandle = (String) source.getParameterValue("registrationHandle");
		
		Node filtersNode = source.getFiltersNode();
		
		if(filtersNode != null)
		{
			Node[] selectNodes = XmlUtil.selectNodes("filter[@required=\"true\"]", filtersNode);
			
			if(selectNodes != null)
			{
				for (Node node : selectNodes) 
				{
					if("registrationHandle".equals(XmlUtil.readAttributeOrNodeString(node, "id")))
					{
						if(registrationHandle == null || "".equals(registrationHandle))
							return;
						else
							break;
					}
				}
			}
		}
		
		// get the Remote Consumer Registration associated with the current registrationHandle
		RemoteConsumerRegistration registration;
		try
		{
			registration = (RemoteConsumerRegistration) ManagerFactory.getEntityManager().find(RemoteConsumerRegistration.class, registrationHandle);
		}
		catch (NoResultException e)
		{
			registration = null;
		}

		// if there is a registration associated with the handle
		// we should populate a list with its clonedPortlets
		if (registration != null)
		{
			ILocalizationManager localizationManager = ManagerFactory.getLocalizationManager();
			for (ClonedPortlet clonedPortlet : registration.getClonedPortlets())
			{
				ISourceData row = source.getData().addRow();
				ServiceInterfaceInstanceConfig serviceInterfaceInstanceConfig = ManagerFactory.getServiceInterfaceInstanceManager().get(sessionConfig, clonedPortlet.getOriginalPortlet().getInterfaceInstanceId(), transaction);
				ServiceInterfaceConfig serviceInterfaceConfig = ManagerFactory.getServiceInterfaceManager().getServiceInterfaceConfig(serviceInterfaceInstanceConfig.getServiceInterfaceId(), transaction);
				String displayName = ManagerFactory.getServiceInterfaceInstanceManager().getDisplayName(serviceInterfaceInstanceConfig,
						transaction);
				row.put("displayName", localizationManager.localize(displayName, serviceInterfaceConfig.getResource()));
				row.put("originalInterfaceInstanceId", clonedPortlet.getOriginalPortlet().getInterfaceInstanceId());
			}

			// apply post load processing
			source.getData().sort(source);
			source.applyPostLoadPagination();
		}
	}
}
